<?php
class Login_wx extends CI_Controller
{	
    public $token_expiration = 300;    //配置token的有效时长

	function __construct() {
        parent::__construct();
    }

    function index(){
    	$data = file_get_contents("php://input");
    	if(!empty($data)){
    		$data = json_decode($data, true);
    		$data = $data['userinfo'];
    		$user_info = get_field_by_where("users", "id AS user_id, child_id, api_secret", "wx_open_id='{$data['openid']}'");
    		if($user_info){
                $child_total = get_field_by_where("user_childrens", "count(id) AS count", "user_id='{$user_info['user_id']}' ");
                $user_info['total_child'] = $child_total['count'];
                $child_info = get_field_by_where("user_childrens", "nick, head_img_url, grade, gender", "id='{$user_info['child_id']}'");
                if(empty($child_info['head_img_url'])){
                    if($child_info['gender'] == 1){
                        $user_info['child_head_img_url'] = get_qiniu_url('FtE8Gn7PqWv0W7aVTuab8RcuGxlm');
                    }else{
                        $user_info['child_head_img_url'] = get_qiniu_url('Fsof6z54HyIM401i0amzqo3VzrF3');
                    }
                }else{
                    $user_info['child_head_img_url'] = $child_info['head_img_url'];
                }
                $user_info['child_nick'] = $child_info['nick'];
    		}else{
    			$md5_pass = md5('000000');//初始密码000000
				$rand_str = '';
				for($i=0; $i<10; $i++){
					$rand_str .= substr($md5_pass, rand(0, strlen($md5_pass)-1), 1);
				}
				$user_data['rand_str'] = $rand_str;
				$user_data['password'] = md5($md5_pass.$rand_str);;
    			$user_data['nick'] = $data['nickname'];
		    	$user_data['wx_open_id'] = $data['openid'];
		    	$user_data['wx_union_id'] = $data['unionid'];
		    	$user_data['head_img_url'] = $data['headimgurl'];
                $user_data['sex'] = $data['sex'];
                $user_data['country'] = $data['country'];
                $user_data['province'] = $data['province'];
                $user_data['city'] = $data['city'];
                $user_data['api_secret'] = create_rand_str(32,32);
		    	$user_data['add_time'] = time();
		    	$this->db->insert("users", $user_data);
                $user_info['user_id'] = $this->db->insert_id();
                $user_info['child_id'] = 0;
                $user_info['total_child'] = 0;  //孩子数量
                $user_info['api_secret'] = $user_data['api_secret'];
                $user_info['child_head_img_url'] = $data['headimgurl'];
                $user_info['child_nick'] = $data['nickname'];
                $user_info['grade'] = '';
                $user_info['gender'] = '';
    		}
            $time = time();
            //写入token数据
            if(!empty($user_info['user_id'])){
                $token = create_rand_str(32, 32);
                $token_data = array("token" => $token, "create_time" => $time, "token_expiration" => $this->token_expiration);
                $token_data['token_expiration_date'] = date("Y-m-d H:i:s", $token_data['create_time']+$token_data['token_expiration']);
                $token_info = get_field_by_where("user_tokens", "id", "user_id = '{$user_info['user_id']}'");
                if(!empty($token_info['id'])){
                    $this->db->update("user_tokens", $token_data, array("user_id" => $user_info['user_id']));
                }else{
                    $token_data['user_id'] = $user_info['user_id'];
                    $this->db->insert("user_tokens", $token_data);
                }
                $token_info = array_merge($user_info, $token_data);
                echo json_encode(array("code" => 200, "token_info" => $token_info));
            }else{
                echo json_encode(array("code" => 404));
            }
    	}
    }
}